package util.concurrent;

import org.junit.Test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.LinkedTransferQueue;

/**
 * 8种 阻塞队列demo
 *
 * 入队
 * put(E e)：如果队列满了，一直阻塞，直到队列不满了或者线程被中断-->阻塞
 *
 * 出队
 * take()：如果队列空了，一直阻塞，直到队列不为空或者线程被中断-->阻塞
 */
public class BlockingQueue {

    @Test
    public void test1() throws InterruptedException {
        LinkedBlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>(1);
        blockingQueue.put(1);
        System.out.println("put1");
        blockingQueue.put(2);
        System.out.println("put2");
        Integer p1 = blockingQueue.take();
        System.out.println("take1");
        Integer p2 = blockingQueue.take();
        System.out.println("take2");
    }

    @Test
    public void test2() throws InterruptedException {
        LinkedTransferQueue<Integer> blockingQueue = new LinkedTransferQueue<>();
        blockingQueue.put(2);
        Integer poll = blockingQueue.take();
        System.out.println(poll);
        Integer p2 = blockingQueue.take();
    }

}
